var phone = "";
var code = "";
var contextPath;
var oldphone=$("#oldphone").val();
$(function(){
	 contextPath = $("#contextPath").val();
	$("#phone").blur(function(){
		phone=this.value;
		phone=$.trim(phone);
	    if(phone!=""&&!/^(13[0-9]|14[0-9]|17[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(phone)){
			layer.open({
			    content:'手机号码格式不正确！',
			    time: 2
			});
			return false;
		}
	});
	$("#code").blur(function(){
		code=this.value;
		code=$.trim(code);
		if(code==""){
			layer.open({
			    content:'请填写验证码!',
			    time: 2
			});
		}
	});
});
function getCode(){
	var id;
	if($.trim(phone)==""){
		layer.open({
		    content:'请填写手机号',
		    time: 2
		});
		return false;
	}else if(!/^(13[0-9]|14[0-9]|17[0-9]|15[0-9]|18[0-9])\d{8}$/i.test(phone)){
		layer.open({
		    content:'手机号码格式不正确！',
		    time: 2
		});
		return false;
	}else if(phone!=oldphone){
		layer.open({
		    content:'该手机号不是您注册的手机号！',
		    time: 2
		});
		return false;
	}
	$.ajax({
		url:"send",
		type:"post",
		data:{"phone":phone,"type":"paypwd"},
		success:function(data){
			if(data.sendNum < 2){
					if(data.result.state == 1 && data.result.code =='100001'){
						countdown(data.result.message,data.sendNum);
					}
			}else{
				$(".ui_btn_success").css({backgroundColor:'#ffffff',color:'#00b18f'});
				$(".ui_btn_success").html('发送次数达到当天限制');
				$(".ui_btn_success").removeClass("sendCode");
			}
		},
		error:function(e){
			 layer.open({
				    content:'获取验证码错误请稍刷新后再试',
				    time: 2
			   });
		}
	});
}
var seTips = function() {
	$(".tips-box").css({'color':'red'});
	setTimeout(function(){
		$(".tips-box").html('');
	}, 2000)
}
var miao;
var  countdown= function (msg,sendNum){
	miao=msg;
	if(sendNum>= 2){
	}else{
		$(".ui_btn_success").css({backgroundColor:'#ffffff',color:'#00b18f'});
		$(".ui_btn_success").html(msg+'秒后重发');
		var time=setInterval(function(){
			if(sendNum > msg){
				window.clearInterval(time);
			}
			msg--;
			if(msg == 0){
				window.clearInterval(time);
				$(".ui_btn_success").css({backgroundColor:'#00b18f',color:'#fff'});
				$(".ui_btn_success").html('获取验证码');
				sendCode();
				msg=miao;
				sendNum++;
				return false;
			}else{
				$(".ui_btn_success").html(msg+'秒后重发');
			}
		}, 1000)
	}
}
function submitPhone(){
	var phone = $("#phone").val();
	var code = $("#code").val();
	if($.trim(phone)==""){
		layer.open({
		    content:'请填写手机号',
		    time: 2
		});
		return false;
	}
	if(phone!=oldphone){
		layer.open({
		    content:'该手机号不是您注册的手机号！',
		    time: 2
		});
		return false;
	}
	if($.trim(code)==""){
		layer.open({
		    content:'请填写验证码!',
		    time: 2
		});
		return false;
	}
	$.ajax({
		url:'checkcode',
		data:{"phone":phone,"type":"paypwd","code":code},
		type:'post',
		dataType:'json',
		success:function(data){
				if(data.result.code == 'error'){
					layer.open({
					    content:'验证码输入错误!',
					    time: 2
					});
					return false;
				}
				if(data.result.code == 'expire'){
					layer.open({
					    content:'验证码已过期!',
					    time: 2
					});
					return false;
				}
				if(data.result.code == 'ok'){
					window.location.href="toUpdatePayPwd";
				}
		},
		error:function(data){
			layer.open({
			    content:data,
			    time: 2
			});
		}
	});
}
